package com.hc.util;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class JDBCUtil {
	//整理重复代码
	//包装前四步
	//1加载驱动	
	//2建立连接	
	//3准备sql
	//4得到执行对象
	
	public static String sql="";
	public static String url="jdbc:mysql://localhost:3306/housecrm";
	public static String user="root";
	public static String pwd="123456";
	public static java.sql.Connection con=null;
	public static PreparedStatement ps=null;
	public static ResultSet rs=null;
	
	 static{//静态代码块
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	public static PreparedStatement getPs() {
		try {
			con=DriverManager.getConnection(url, user, pwd);
			ps=con.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return ps;
	}
	
	//关闭资源
	public static void close() {
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
}
